package cn.ckx.coding.config;

import cn.ckx.coding.common.utils.http.HttpContextUtils;
import cn.ckx.coding.properties.AppProperties;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Objects;

/**
 * mybatis-plus配置
 * 
 * @author kunxin.cai
 */
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
    
    private final AppProperties appProperties; 
    
    public MybatisPlusConfig(AppProperties appProperties){
        this.appProperties = appProperties;     
    }
    
    
    /**
     * 更新或者删除
     * @return MetaObjectHandler
     */
    @Bean
    public MetaObjectHandler metaObjectHandler(){
        return new MetaObjectHandler() {
            @Override
            public void insertFill(MetaObject metaObject) {
                Date now = new Date();
                this.setFieldValByName("createTime", now, metaObject);
                this.setFieldValByName("updateTime", now, metaObject);
                
                String userId = getUserId();
                this.setFieldValByName("createId", userId, metaObject);
                this.setFieldValByName("updateId", userId, metaObject);
            }

            @Override
            public void updateFill(MetaObject metaObject) {
                this.setFieldValByName("updateTime", new Date(), metaObject);
                this.setFieldValByName("updateBy", getUserId(), metaObject);
            }
        };
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        paginationInnerInterceptor.setOverflow(true);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }
    
    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题
     */
    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);
    }
    
    /**
     * 
     * 防止恶意更新或删除
     */
    @Bean
    public BlockAttackInnerInterceptor blockAttackInnerInterceptor(){
        return new BlockAttackInnerInterceptor();
    }

    private String getUserId() {
        // TODO: 2021/3/2 是否确定把userId放在请求头中呢？ 
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); 
        if(Objects.nonNull(request)) {
            String userId = request.getHeader(appProperties.getLogin().getUserId());
            if (StringUtils.isBlank(userId)){
                userId = (String) request.getAttribute(appProperties.getLogin().getUserId());        
            }
            if (StringUtils.isNotBlank(userId)){
                return userId;
            }
        }
        return "";
    }


}
